package bTree.LeetCode;

import java.util.HashSet;
import java.util.Set;

public class 数组嵌套 {
    int res = 0;

    public int arrayNesting(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            if (!set.contains(nums[i])) {
                dfs(set, nums, i,0);

            }
        }
        return res;
    }

    private void dfs(Set<Integer> set, int[] nums, int i,int cnt) {
        if (set.contains(nums[i])) {
            res = Math.max(res, cnt);
            return;
        }
        set.add(nums[i]);
        dfs(set, nums, nums[i],cnt+1);
    }
}